package com.dhcc.bpm.common.utils.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: RestHttpUtil
 * @Description: restTemp发送请求工具类
 * @author
 * @date 2021-08-11 09:16:39
 */
@Slf4j
@Component
public class RestHttpUtil {

    /**
     * @Title: sendPost
     * @Description: 发送post请求网关，请求行内manage服务
     * @author xfy
     * @date 2021-08-11 09:39:01 
     * @param url
     * @param requestData 
     * @return 返回JsonBaseResult对象
     */
	public JSONObject sendPost(String url, JSONObject requestData) {
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(30000);
		requestFactory.setReadTimeout(30000);
		RestTemplate restTemplate = new RestTemplate(requestFactory);

		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);

		HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(requestData),httpHeaders);
		log.info("请求url：{}", url);
		log.info("请求数据：{}", requestData);

		try {
			JSONObject resultJSON = restTemplate.postForObject(url, entity, JSONObject.class);
			
			log.info("返回数据：" + resultJSON);
			if (null == resultJSON || resultJSON.isEmpty()) {
				log.error("请求服务响应结果为空");
				return null;
			}
			return resultJSON;
		} catch (RestClientException e) {
			log.error("请求服务超时!-异常：{}", e);
			log.info("请求服务超时!-异常：{}", e);
			return null;
		}
        
	}

		
}
